<?php
/**
 * Created by PhpStorm.
 * User: dyane
 * Date: 2018/12/17
 * Time: 9:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
use think\Validate;

class Book extends Base
{

    public function index()
    {
        return view();
    }

    public function getBookList()
    {
        $param = $this->request->param();
        $where = [
            'delete_ts'=>0
        ];
        if (!empty($param['name'])) {
            $where['name'] = ['like','%' . $param['name'] .'%'];
        }
        $count = Db::name('book')->where($where)->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('book')
            ->where($where)
            ->limit($tol,$pageSize)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['created_ts'] = date('Y-m-d H:i:s',$value['created_ts']);
            $list[$key]['update_ts'] = date('Y-m-d H:i:s',$value['update_ts']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    public function bookInfo()
    {
        $param = $this->request->param();
        if (!empty($param['id'])) {
            $info = Db::name('book')->where(['id' => $param['id']])->find();
            $this->assign('info', $info);
        }
        return view();
    }


    public function bookAdd()
    {
        $param = $this->request->param();
        $rule = [
            'name|课程名称'=>'require',
            'times|课程次数'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }

        if($param['times'] <= 0){
            return json(['code'=>400,'msg'=>'课程次数错误']);
        }

        if(!empty($param['id'])){
            $data = [
                'update_ts'=>time(),
                'times'=>$param['times'],
                'name'=>$param['name']
            ];
            $res = Db::name('book')->where(['id'=>$param['id']])->update($data);
        }else{
            $data = [
                'name'=>$param['name'],
                'times'=>$param['times'],
                'created_ts'=>time(),
                'update_ts'=>time(),
                ];
            $res = Db::name('book')->insert($data);
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }


    public function deleteBook()
    {
        $param = $this->request->param();
        $res = Db::name('book')->where(['id'=>$param['id']])->update(['delete_ts'=>time()]);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }



    public function userIndex()
    {
        return view();
    }


    public function getUserBookList()
    {
        $param = $this->request->param();
        $where = [
            'users.delete_ts'=>0
        ];
        if (!empty($param['name'])) {
            $where['book.name'] = ['like','%' . $param['name'] .'%'];
        }
        if (!empty($param['user_name'])) {
            $where['users.user_name'] = ['like','%' . $param['user_name'] .'%'];
        }
        $count = Db::name('user_book')
            ->join('users','users.id=user_book.user_id','left')
            ->join('book','book.id=user_book.book_id','left')
            ->where($where)
            ->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('user_book')
            ->where($where)
            ->join('users','users.id=user_book.user_id','left')
            ->join('book','book.id=user_book.book_id','left')
            ->limit($tol,$pageSize)
            ->field('user_book.*,users.user_name,book.name')
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['created_ts'] = date('Y-m-d H:i:s',$value['created_ts']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

}